package tv.youxing.android.ui;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import tv.youxing.android.MainActivity;
import tv.youxing.android.R;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.ShareUtils;

/**
 * 欢迎界面 用来做 界面初始化
 */
public class WelComeActivity extends Activity {

    TextView mCountDown;//倒计时
    private final static int COUNT = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_wel_come);

        mCountDown = findViewById(R.id.down_count);

        //initView();


        if (!ShareUtils.getBoolean(this, AppConst.ISGUIDE, false)) {
            startActivity(new Intent(this,GuideActivity.class));
            finish();
            return;
        }

        //申请权限
        getAuthority();
    }


    /**
     * 申请权限回调
     */
    PermissionListener mPermissionListener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
            if (requestCode == 100) {
                System.out.println("成功");
                initView();
            }
        }

        @Override
        public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
            if (requestCode == 100) {
               // CustomToast.INSTANCE.showToast("请设置权限,否者将影响正常使用");
                // 是否有不再提示并拒绝的权限。
                if (AndPermission.hasAlwaysDeniedPermission(WelComeActivity.this, deniedPermissions)) {
                    // 第一种：用AndPermission默认的提示语。
                    AndPermission.defaultSettingDialog(WelComeActivity.this, 400).show();
                } else {
                    getAuthority();
                }
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 400: {
                Toast.makeText(this,"设置成功", Toast.LENGTH_LONG).show();
                initView();
                break;
            }
        }
    }


    @Override
    public int checkSelfPermission(String permission) {


        return super.checkSelfPermission(permission);

    }

    void getAuthority() {
        // 申请多个权限。
        AndPermission.with(this)
                .requestCode(100)
                .permission(Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .callback(mPermissionListener)
                // rationale作用是：用户拒绝一次权限，再次申请时先征求用户同意，再打开授权对话框；
                // 这样避免用户勾选不再提示，导致以后无法申请权限。
                // 你也可以不设置。
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(WelComeActivity.this, rationale)
                                .show();
                    }
                })
                .start();
    }


    private void initView() {
        final Timer timer = new Timer();
        final long end = System.currentTimeMillis() + 1000 * 3;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(COUNT);

            }
        }, 0, 1000); // 延迟运行的时间，倒计时每次的时间
        //这里的schedule的第二个参数意义是到了这个时间尽快运行run里面的方法
        timer.schedule(new TimerTask() {

            @Override
            public void run() {

                startActivity(new Intent(WelComeActivity.this, MainActivity.class));

//                //界面跳转
//                if (ShareUtils.getBoolean(WelComeActivity.this, AppConst.ISGUIDE, false) == false) {
//                    startActivity(new Intent(WelComeActivity.this, GuideActivity.class));
//                } else {
//                    startActivity(new Intent(WelComeActivity.this, MainActivity.class));
//                }
                finish();
                timer.cancel();
            }
        }, new Date(end));

    }

    private Handler handler = new Handler() {
        int num = 2;

        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case COUNT:
                    mCountDown.setText(String.valueOf(num));
                    num--;
                    break;

                default:
                    break;
            }
        }

        ;
    };
}
